package com.wl.application.controller;

import com.alibaba.cloud.nacos.discovery.NacosServiceDiscovery;
import com.alibaba.nacos.api.exception.NacosException;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;
import java.util.Map;

/**
 * @author wl
 * @version 1.0
 * @date 2022/7/21
 * @description:
 */
@RestController
@RequestMapping("/consumer")
public class ConsumerController {
    @Autowired
    private NacosServiceDiscovery nacosServiceDiscovery;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/hello")
    public String hello() throws NacosException {
        String forObject = restTemplate.getForObject("http://" + "01-provider/provider/hello", String.class);
        return "hello" + System.currentTimeMillis() + "调用远程接口的返回值：" + forObject;
    }

    @PostMapping("/save")
    public Map save(@RequestBody Map<String, Object> map) throws NacosException {
        //调用远程api接口
        Map map1 = restTemplate.postForObject("http://01-provider/provider/save", map, Map.class);
        System.out.println(map);
        System.out.println(map1);
        return map1;
    }
    private static int flag=1;

    @PostMapping("/del/{id}")
    public String del(@PathVariable Integer id) throws NacosException {
        //调用远程api接口
        String str1 = restTemplate.postForObject("http://01-provider/provider/del/"+id, null, String.class);
        System.out.println(id);
        System.out.println(str1);
        return str1;
    }
}
